//
//  JoServer.swift
//  JoTravel
//
//  Created by tong on 15/11/26.
//  Copyright © 2015年 zhuxietong. All rights reserved.
//

import Foundation

public let __tag_colors = ["#F86620","#169543","#1abc9c","#2ecc71","#3498db","#9b59b6","#34495e","#16a085","#27ae60","#2980b9","#8e44ad","#8e44ad","#2c3e50","#f1c40f","#e67e22","#e74c3c","#95a5a6","#f39c12","#d35400","#c0392b","#7f8c8d"]


public let __tags = ["小白","滑雪","游泳","跑步","编程","白菜","英雄联盟","美食汇","约","马拉松","斯诺克",]


public var __images = [
    "http://page.firstvar.com/media/img/0000000001.jpg", "http://page.firstvar.com/media/img/0000000002.jpg", "http://page.firstvar.com/media/img/0000000003.jpg", "http://page.firstvar.com/media/img/0000000004.jpg", "http://page.firstvar.com/media/img/0000000005.jpg", "http://page.firstvar.com/media/img/0000000006.jpg", "http://page.firstvar.com/media/img/0000000007.jpg", "http://page.firstvar.com/media/img/0000000008.jpg", "http://page.firstvar.com/media/img/0000000009.jpg", "http://page.firstvar.com/media/img/0000000010.jpg", "http://page.firstvar.com/media/img/0000000011.jpg", "http://page.firstvar.com/media/img/0000000012.jpg", "http://page.firstvar.com/media/img/0000000013.jpg", "http://page.firstvar.com/media/img/0000000014.jpg", "http://page.firstvar.com/media/img/0000000015.jpg", "http://page.firstvar.com/media/img/0000000016.jpg", "http://page.firstvar.com/media/img/0000000017.jpg", "http://page.firstvar.com/media/img/0000000019.jpg", "http://page.firstvar.com/media/img/0000000020.jpg", "http://page.firstvar.com/media/img/0000000021.jpg", "http://page.firstvar.com/media/img/0000000022.jpg", "http://page.firstvar.com/media/img/0000000023.jpg", "http://page.firstvar.com/media/img/0000000024.jpg"];



public var __heads = [
    "http://page.firstvar.com/media/avatar/avatar1.jpg", "http://page.firstvar.com/media/avatar/avatar2.jpg", "http://page.firstvar.com/media/avatar/avatar3.jpg", "http://page.firstvar.com/media/avatar/avatar4.png", "http://page.firstvar.com/media/avatar/avatar5.jpg", "http://page.firstvar.com/media/avatar/avatar6.jpg", "http://page.firstvar.com/media/avatar/avatar7.jpg", "http://page.firstvar.com/media/avatar/avatar8.jpg", "http://page.firstvar.com/media/avatar/avatar9.jpg", "http://page.firstvar.com/media/avatar/avatar10.jpg"];


public var __logos = [
    "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3547592982,1978197711&fm=200&gp=0.jpg",
    "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1443299318,121744606&fm=27&gp=0.jpg",
    "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1017980852,3420383746&fm=27&gp=0.jpg",
    "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1252256153,3419487072&fm=27&gp=0.jpg",
    "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1895838255,3248645471&fm=27&gp=0.jpg",
    "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=871295046,2601159057&fm=27&gp=0.jpg",
    "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3255919467,4117243677&fm=27&gp=0.jpg"
]

public var __names = ["Jo Developer","黑老头","野牛","清风","子皓","葫芦侠","vim"]
public var __titles = ["丽江玉龙雪山一日游","腾冲温泉SPA","西双版纳泼水节活动","香格里拉－迪庆3日游","大理－丽江－梅里雪山","石林奇观","澄江烧烤晚会"]

extension Array
{
    
    public var stringV:String{
        get{
            var ls = [String]()
            for a in self
            {
                ls.append("\(a)")
            }
            return ls.joined(separator: "")
        }
    }
    
    public func one()->Any
    {
        let index = Int(arc4random() % UInt32(self.count))
        return self[index]
    }
    
    public var one_str:String
    {
        get{
            
            let index = Int(arc4random() % UInt32(self.count))
            let str = "\(self[index])"
            return str
        }
    }
    
    public func some1(_ max:Int=10) ->Array
    {
        var num = Int(arc4random() % UInt32(max))
        
        var list = Array()
        
        repeat
        {
            let index = Int(arc4random() % UInt32(self.count))
            list.append(self[index])
            
            num -= 1
        }while(num>0)
        
        return list
    }
    
    
    public func some(_ max:Int=10) ->Array
    {
        var num = Int(arc4random() % UInt32(max))+1
        
        var list = Array()
        
        repeat
        {
            let index = Int(arc4random() % UInt32(self.count))
            list.append(self[index])
            
            num -= 1
        }while(num>0)
        
        return list
    }
    
    public func creat_info(_ max:Int=5) ->String
    {
        var index = Int(arc4random() % UInt32(max))
        
        var str = self.one_str
        repeat{
            str = self.one_str + str
            index -= 1
        }while(index>0)
        return str
    }
    
    public func have(str string:String)->Bool
    {
        for one in self
        {
            if let s = one as? String
            {
                if s == string
                {
                    return true
                }
            }
        }
        
        return false
    }
    
    
}



public class SandBox: NSObject {
    
    
    class public func random<T>(count:Int,source:[T])->[T]
    {
        if count >= source.count
        {
            return source
        }
        var newsource = source
        var list = [T]()
        while list.count < count {
            let one = SandBox.one(source: newsource)
            list.append(one.0)
            newsource.remove(at: one.1)
        }
        return list
        
    }
    
    class public func one<T>(source:[T])->(T,Int)
    {
        let new_souce = source
        let index = Int(arc4random() % UInt32(new_souce.count))
        return (source[index],index)
    }
    
    public class func one()-> NSMutableDictionary {
        var one = [String:Any]()
        one["title"] = __titles.one()
        one["subTitle"] = __titles.creat_info(20)
        one["conent"] = __titles.creat_info(20)
        one["avatar"] = __heads.one_str
        one["username"] = __names.one_str

        one["img"] = __images.one()
        return one.mutable_dictionary
    }
    
    //    public class func object() ->[String:AnyObject]
    //    {
    //        var one = [String:AnyObject]()
    //        one["title"] = __titles.one_str
    //        one["subTitle"] = __titles.creat_info(20)
    //        one["img"] = __images.one_str
    //        return one
    //    }
    
    
    public static var object:[String:Any] {
        get{
            var one = [String:Any]()
            one["title"] = __titles.one()
            one["subTitle"] = __titles.creat_info(20)
            one["conent"] = __titles.creat_info(20)
            one["avatar"] = __heads.one_str
            one["username"] = __names.one_str

            one["img"] = __images.one()
            return one
        }
    }
    
    public class func defaultItems(num:Int=5)->[NSMutableDictionary] {
        
        var list = [NSMutableDictionary]()
        for _ in 0..<num
        {
            list.append(SandBox.one())
        }
        return list
    }
    
    
    
    public class func userItem()-> NSMutableDictionary {
        var one = [String:Any]()
        one["name"] = __names.one()
        one["img"] = __heads.one()
        one["content"] = __titles.creat_info(20)
        
        return one.mutable_dictionary
    }
    
    public class func user()-> [String:String] {
        var one = [String:String]()
        one["name"] = __names.one_str
        one["img"] = __heads.one_str
        return one
    }
    
    public class func item()-> [String:String] {
        var one = [String:String]()
        one["title"] = __titles.one_str
        one["subTitle"] = __titles.one_str
        one["img"] = __images.one_str
        return one
    }
    
    
    public class func random(min:UInt32,max:UInt32)->UInt32{
        return  arc4random_uniform(max-min)+min
    }
    
    public class func ID(len:Int=10)->String{
        let min:UInt32=33,max:UInt32=127
        var string=""
        for _ in 0..<len{
            
            let c = Character(UnicodeScalar(SandBox.random(min: min,max:max))!)
            string.append(c)
        }
        return string
        
    }
    
    
}

extension SandBox{
    public class func section(count:Int)-> [[String:String]] {
        var items = [[String:String]]()
        for _ in 0..<count
        {
            let one = ["title":__titles.one_str,"subTitle":__titles.some(10).stringV,"img":__images.one_str,"head":__heads.one_str]
            items.append(one as [String : String])
        }
        return items
    }
}


//JoClient().requestPageData(1, size: 3, time: 1, itemCreator: {
//    () in return SandBox.default_item()
//    }) { (list) -> Void in
//        self.loadingV?.dismiss()
//        print(list)
//}


//public func delay(delay:TimeInterval, closure:@escaping ()->Void) {
//    
//    
//    DispatchQueue.userInteractive.after(delay, execute: closure)
//    
//    
////    let delayTime = DispatchTime.now().rawValue + delay
////    
////    
////    DispatchQueue.main.after(delayTime, execute: closure)
//    
//}

public func co_delay(delay:Double, closure:@escaping ()->Void) {
    
    
    //    let delayTime = DispatchWallTime.now() + delay
    //    DispatchQueue.main.after(delayTime, execute: closure)
    
    let delayTime = DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
    DispatchQueue.main.asyncAfter(deadline: delayTime) {
        closure()
    }
    
}

public typealias DelayTaskBlock = ()->Void;



public class JoClient {
    
    public static var task:DelayTaskBlock? = nil
    public static func delay(delay:Double,closure:@escaping ()->Void){
        
        task = closure
        let delayTime = DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
        DispatchQueue.main.asyncAfter(deadline: delayTime) {
            JoClient.task?()
        }
    }
    
    
    
    public func requestPageData<T>(
        page:Int = 0,
        size:Int = 10,
        time:UInt32 = 1,
        itemCreator:@escaping ()->T = { return "hello" as! T},
        sucess:@escaping (_ list:[T]) ->Void
    )
    {
        
        DispatchQueue(label: "swift_client").async {
            sleep(time)
            var list = [T]()
            for _ in 0..<size
            {
                list.append(itemCreator())
            }
            DispatchQueue.main.async(execute: {
                sucess(list)
            })
        }
        
        //        DispatchQueue.global(attributes: DispatchQueue.GlobalAttributes.qosDefault).async {
        //            sleep(time)
        //            var list = [T]()
        //            for _ in 0..<size
        //            {
        //                list.append(itemCreator())
        //            }
        //            DispatchQueue.main.async(execute: {
        //                sucess(list: list)
        //            })
        //
        //        }
    }
}
